package UIFree.components
{
	import UIFree.ObjectEvent;
	import UIFree.uif_app;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	public class uif_shortKeyManager extends EventDispatcher {
		private var _list: Array;
		private var _app: uif_app;
		
		public static const ADDSHORTKEY		: String 	= 'ADDSHORTKEY';
		public static const REMOVESHORTKEY	: String 	= 'REMOVESHORTKEY';
		public static const SHORTKEYDOWN	: String 	= 'SHORTKEYDOWN';
		
		public function uif_shortKeyManager(a_app: uif_app) {
			super();
			_app = a_app;
			initialize();
		}
		
		protected function initialize(): void {
			_list = new Array();
			listenEvents();
		}
		
		protected function listenEvents(): void {
			_app.stage.addEventListener(KeyboardEvent.KEY_DOWN, doKeyDown);
			_app.addEventListener(ADDSHORTKEY, doAddShortKey);
			_app.addEventListener(REMOVESHORTKEY, doRemoveShortKey);
		}
		
		protected function doAddShortKey(e: ObjectEvent): void {
			addTarget(e.object as String, e.target as EventDispatcher);
			e.stopPropagation();
		}
		
		protected function doRemoveShortKey(e: Event): void {
			removeTarget(e.target as EventDispatcher);
		}
		
		protected function indexOf(target: EventDispatcher): int {
			for (var i: int=0; i<_list.length; i++) {
				if (_list[i].target == target) return i; 
			}

			return -1;
		}
		
		protected function itemOf(target: EventDispatcher) : Object {
			var index: int = indexOf(target);
			if (index > -1) return _list[index];
			return null;
		}
		
		public function addTarget(a_shortKey: String, a_target: EventDispatcher): void {
			a_target.addEventListener(uif_ecomponent.DISPOSE, doDisposeComponent);
			_list.push(uif_util.union({
				target		: a_target
			}, parseShortKeyString(a_shortKey)));
		}
		
		public function removeTarget(target: EventDispatcher): void {
			var index: int = indexOf(target);
			if (index > -1) {
				target.removeEventListener(uif_ecomponent.DISPOSE, doDisposeComponent);
				_list.splice(index, 1);
			}
		}
		
		protected function parseShortKeyString(a_shortKey: String): Object {
			var a		: Array = a_shortKey.split('+');
			var result	: Object = {
				ctrl: false,
				shift: false,
				alt: false
			};
			function updateP(index: int): void {
				var s: String = a[index].toUpperCase();
				result.ctrl 	= result.ctrl 	|| (s == 'CTRL');
				result.shift 	= result.shift 	|| (s == 'SHIFT');
				result.alt 		= result.alt 	|| (s == 'ALT');
				if ((Keyboard as Object).hasOwnProperty(s))
					result.keyCode  = Keyboard[s];
			}
			
			for (var i: int=0; i<a.length; i++) 
				updateP(i);
			return result;
		}
		
		protected function doDisposeComponent(e: Event): void {
			removeTarget(e.target as EventDispatcher);
		}
		
		protected function doKeyDown(e: KeyboardEvent): void {
			if (!(e.target is TextField)) {
				for (var i: int=0; i<_list.length; i++) {
					if ((_list[i].keyCode == e.keyCode) &&
						(_list[i].ctrl == e.ctrlKey) &&
						(_list[i].shift == e.shiftKey) &&
						(_list[i].alt == e.altKey)) {
						_list[i].target.dispatchEvent(new Event(SHORTKEYDOWN));
						e.stopPropagation();
					}
				}
			}
		}
	}
}